package com.example.backend.service.serviceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.Function;
import com.example.backend.entity.FunctionVO;
import com.example.backend.entity.User;
import com.example.backend.entity.UserVO;
import com.example.backend.mapper.FunctionMapper;
import com.example.backend.service.FunctionService;
import com.example.backend.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Jcy
 * @Date 2022/5/6
 */
@Component
public class FunctionServiceImpl implements FunctionService {
    @Autowired
    private FunctionMapper functionMapper;
    @Override
    public PageUtils getFunctions(int page, int pageSize){
        Page<FunctionVO> p = new Page<>(page, pageSize);
        p.setRecords(this.functionMapper.getFunction(p));
        return new PageUtils(p);
    }
    @Override
    public int addReduce(int functionId){
        Function function = functionMapper.selectById(functionId);
        if(functionMapper.selectById(functionId)==null){
            return 1;
        }
        else if(functionMapper.selectById(functionId)!=null){
            if(function.getFunction_status()==1){
                int functionStatus0=0;
                function.setFunction_status(functionStatus0);
                functionMapper.updateById(function);
                return 0;
            }
            else if(function.getFunction_status()==0){
                int functionStatus1=1;
                function.setFunction_status(functionStatus1);
                functionMapper.updateById(function);
                return 0;
            }
        }
        return 2;
    }
    public int modifyFunction(int functionId,String functionName){
        Function function = functionMapper.selectById(functionId);
        if(functionMapper.selectById(functionId)==null){
            return 1;
        } else{
            function.setFunction_name(functionName);
            functionMapper.updateById(function);
            return 0;
        }
    }
    public int getFunctionStatus(int functionId){
        Function function = functionMapper.selectById(functionId);
        if(function.getFunction_status()==0)
            return 0;
        else{
            return 1;
        }
    }
}
